import { createReducer, Action, on } from '@ngrx/store';
import { PlayMode } from '@share/wyy-ui/wyy-player/player-types';
import { Song } from '@services/data-types/common.type';
import { setPlaying, setPlayList, setSongList, setPlayMode, setCurrentIndex, setCurrentAction } from '@store/actions/player.actions';

export enum CurrentActions {
  Add,
  Play,
  Delete,
  Clear,
  Other
}

export interface PlayState {
  // 是否在播放
  playing: boolean;
  // 播放模式
  playMode: PlayMode;
  // 当前播放歌曲的索引
  currentIndex: number;
  // 歌曲列表
  songList: Song[];
  // 播放列表
  playList: Song[];
  currentAction: CurrentActions;
}

export const initialState: PlayState = {
  playing: false,
  playMode: { type: 'loop',  label: '循环' },
  currentIndex: -1,
  songList: [],
  playList: [],
  currentAction: CurrentActions.Other
};

const reducer = createReducer(
  initialState,
  on(setPlaying, (state, { playing }) => ({...state, playing})),
  on(setSongList, (state, { songList }) => ({...state, songList})),
  on(setPlayMode, (state, { playMode }) => ({...state, playMode})),
  on(setPlayList, (state, { playList }) => ({...state, playList})),
  on(setCurrentIndex, (state, { currentIndex }) => ({...state, currentIndex})),
  on(setCurrentAction, (state, { currentAction }) => ({...state, currentAction}))
);

export function playerReducer(state: PlayState, action: Action) {
  return reducer(state, action);
}
